package com.xiaoshuidi.cloud.module.bpm.job.delayjob.strategy;

import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.bpm.controller.admin.swo.vo.TicketDelayJobParamDTO;
import com.xiaoshuidi.cloud.module.bpm.dal.dataobject.swo.SwoTicketDO;
import com.xiaoshuidi.cloud.module.bpm.dal.mysql.swo.SwoTicketMapper;
import com.xiaoshuidi.cloud.module.bpm.service.message.BpmMessageService;
import com.xiaoshuidi.cloud.module.bpm.service.message.dto.MessageWhenSwoTicketRemindReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * 维修工单-受理超时提醒
 */
@Slf4j
@Component
public class ReminderOfAcceptanceTimeoutStrategy implements IStrategy {

    @Resource
    private SwoTicketMapper swoTicketMapper;
    @Resource
    private BpmMessageService bpmMessageService;

    @Override
    public void consumerTask(Object taskParam) {
        log.info("维修工单-受理超时提醒任务执行, 当前时间:{}, 参数:{}", LocalDateTime.now(), JsonUtils.toJsonString(taskParam));
        TicketDelayJobParamDTO ticketDelayJobParamDTO = (TicketDelayJobParamDTO) taskParam;
        SwoTicketDO swoTicket = swoTicketMapper.selectById(ticketDelayJobParamDTO.getSwoTicketId());
        // 检查受理人有没有变动, 如果当前受理人不是发起延迟任务时的处理人, 则不需要通知
        if(!ticketDelayJobParamDTO.getAssigneeId().equals(swoTicket.getAssignee())){
            return;
        }
        // 如果已受理,则不需要短信提醒
        if(swoTicket.getIsAcceptance()){
            return;
        }
        // 未受理, 则需要短信提醒
        MessageWhenSwoTicketRemindReqDTO swoTicketRemindReqDTO = new MessageWhenSwoTicketRemindReqDTO()
                .setUserId(swoTicket.getAssignee())
                .setParam("即将受理超时，")
                .setRoomFullName(swoTicket.getRoomFullName());
        bpmMessageService.sendMessageWhenSwoTicketRemind(swoTicketRemindReqDTO);
        log.info("为维修工单ID:{}发送受理超时短信提醒", ticketDelayJobParamDTO.getSwoTicketId());
    }
}
